package com.baiyu.common.utils;

/**
 * @author 16225
 */
public class LatLonUtils {

    /**
     * π
     */
    private static final double PI = 3.14159265358979323;

    /**
     * 赤道半径
     */
    private static final double EARTH_RADIUS = 6378137;

    private static final double RAD = Math.PI / 180.0;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 根据中心点经纬度及半径范围计算出最大及最小经纬度
     *
     * @param lon    经度
     * @param lat    纬度
     * @param raidus 单位米
     * @return 最大及最小经纬度
     */
    public static double[] getAround(double lon, double lat, int raidus) {

        Double latitude = lat;
        Double longitude = lon;

        Double degree = (24901 * 1609) / 360.0;
        double raidusMile = raidus;

        Double dpmLat = 1 / degree;
        Double radiusLat = dpmLat * raidusMile;
        Double minLat = latitude - radiusLat;
        Double maxLat = latitude + radiusLat;

        Double mpdLng = degree * Math.cos(latitude * (PI / 180));
        Double dpmLng = 1 / mpdLng;
        Double radiusLng = dpmLng * raidusMile;
        Double minLng = longitude - radiusLng;
        Double maxLng = longitude + radiusLng;
        System.out.println("[" + minLat + "," + minLng + "," + maxLat + "," + maxLng + "]");
        return new double[]{minLng, minLat, maxLng, maxLat};
    }

    /**
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
     *
     * @param lon1 最大经度
     * @param lat1 最大纬度
     * @param log2 最小经度
     * @param lat2 最小纬度
     * @return 单位米
     */
    public static double getDistance(double lon1, double lat1, double log2, double lat2) {
        double radLat1 = lat1 * RAD;
        double radLat2 = lat2 * RAD;
        double a = radLat1 - radLat2;
        double b = (lon1 - log2) * RAD;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     *
     * @param origin      出发点（经度，纬度）
     * @param destination 目的地（经度，纬度）
     * @return 单位米
     */
    public static double getDistance(String origin, String destination) {
        String[] temp = origin.split(",");
        String[] temp1 = destination.split(",");
        if (temp.length == 2 && temp1.length == 2) {
            return getDistance(temp[0], temp[1], temp1[0], temp1[1]);
        }
        return 99999;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     *
     * @param originLon      出发点经度
     * @param originLat      出发点纬度
     * @param destinationLon 目的地经度
     * @param destinationLat 目的地纬度
     * @return 单位米
     */
    public static double getDistance(String originLon, String originLat, String destinationLon, String destinationLat) {
        double radLat1 = rad(Double.parseDouble(originLat));
        double radLat2 = rad(Double.parseDouble(destinationLat));
        double a = radLat1 - radLat2;
        double b = rad(Double.parseDouble(originLon)) - rad(Double.parseDouble(destinationLon));
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        // 保留两位小数
        s = Math.round(s * 100d) / 100d;
        s = s * 1000;
        return s;
    }

}
